package com.wolfword.crawler.server.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;




public class ApplicationReady implements
		ApplicationListener<ApplicationReadyEvent>, ApplicationContextAware {
	
	private static final Logger logger = LoggerFactory
			.getLogger(ApplicationReady.class);
	
	// Spring应用上下文环境
	private static ApplicationContext applicationContext;

	public void onApplicationEvent(ApplicationReadyEvent event) {
		logger.info("服务端已经正常启动");
		//爬取网站
//		Crawler crawler = new Crawler();
//		crawler.crawlHtmls();
		//new Thread(new Crawler()).start();
		/*threadPool.execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (true) {
					
					
				}
			}
		});*/
		//new ChatServer().connect();
	}
	
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		// TODO Auto-generated method stub

		ApplicationReady.applicationContext = applicationContext;
	}

	/**
	 * @return ApplicationContext
	 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/**
	 * 获取对象
	 * 
	 * @param name
	 * @return Object
	 * @throws BeansException
	 */
	public static Object getBean(String name) throws BeansException {
		logger.info("bean:" + name + "是否包含"
				+ applicationContext.containsBean(name) + "");
		Object o = applicationContext.getBean(name);
		if (o != null) {
			logger.info(o.getClass().getName());
		}
		return o;
	}

}